<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use clt\Leftnav;
use app\admin\model\System as SysModel;
class System extends Common
{
    /********************************站点管理*******************************/
    //站点设置
    public function system($sys_id=1){
        $table = db('system');
        if(request()->isPost()) {
            $datas = input('post.');
            if($table->where('id',1)->update($datas)!==false) {
                savecache('System');
                return json(['code' => 1, 'msg' => '站点设置保存成功!', 'url' => url('system/system')]);
            } else {
                return json(array('code' => 0, 'msg' =>'站点设置保存失败！'));
            }
        }else{
            $system = $table->field('id,name,url,title,key,des,bah,copyright,ads,tel,email,logo,buy_tips')->find($sys_id);
            $this->assign('system', $system);
            return $this->fetch();
        }
    }
    public function email(){
        $table = db('config');
        if(request()->isPost()) {
            $datas = input('post.');
            foreach ($datas as $k=>$v){
                $table->where(['name'=>$k,'inc_type'=>'smtp'])->update(['value'=>$v]);
            }
            return json(['code' => 1, 'msg' => '邮箱设置成功!', 'url' => url('system/email')]);
        }else{
            $smtp = $table->where(['inc_type'=>'smtp'])->select();
            $info = convert_arr_kv($smtp,'name','value');
            $this->assign('info', $info);
            return $this->fetch();
        }
    }
    public function trySend(){
        $sender = input('email');
        //检查是否邮箱格式
        if (!is_email($sender)) {
            return json(['code' => -1, 'msg' => '测试邮箱码格式有误']);
        }
        $send = send_email($sender, '测试邮件', '您好！这是一封来自'.$this->system['name'].'的测试邮件！');
        if ($send) {
            return json(['code' => 1, 'msg' => '邮件发送成功！']);
        } else {
            return json(['code' => -1, 'msg' => '邮件发送失败！']);
        }
    }
  public function sms()
    {
        $table = db('config');
        if(request()->isPost()) {
            $datas = input('post.');
            foreach ($datas as $k=>$v){
                $table->where(['name'=>$k,'inc_type'=>'dy_sms'])->update(['value'=>$v]);
            }
            return json(['code' => 1, 'msg' => '短信设置成功!', 'url' => url('system/sms')]);
        }else{
            $dy_sms = $table->where(['inc_type'=>'dy_sms'])->select();
            $info = convert_arr_kv($dy_sms,'name','value');
            $this->assign('info', $info);
            return $this->fetch();
        }     
    }
    
   public function rule()
    {
        $table = db('config');
        if(request()->isPost()) {
            $datas = input('post.');
            foreach ($datas as $k=>$v){
                $table->where(['name'=>$k,'inc_type'=>'rule'])->update(['value'=>$v]);
            }
            return json(['code' => 1, 'msg' => '规则设置成功!', 'url' => url('system/rule')]);
        }else{
            $rule = $table->where(['inc_type'=>'rule'])->select();
            $info = convert_arr_kv($rule,'name','value');
            $this->assign('info', $info);
            return $this->fetch();
        }     
    }
    
   public function get_rule(){
      if (request()->isPost()){
        $page    = input('page') ? input('page') : 1;
        $pageSize= input('limit')?input('limit'):config('pageSize');
        $list    = db('rule')
		         ->order('sort')
                 ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                 ->toArray();
        return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
      }
   }
   
   public function get_upgrade(){
      if (request()->isPost()){
        $page    = input('page') ? input('page') : 1;
        $pageSize= input('limit')?input('limit'):config('pageSize');
        $list    = db('upgrade')
                 ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                 ->toArray();
        return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
      }
   }
   
   public function set_rule_val(){
       $id  = input('post.id');
       $key = input('post.key');
       $val = input('post.val');
       
       $map['id'] = $id;
       $map[$key] = $val;
       db('rule')->update($map);
   }
   
   public function set_upgrade_val(){
       $id  = input('post.id');
       $key = input('post.key');
       $val = input('post.val');
       
       $map['id'] = $id;
       $map[$key] = $val;
       db('upgrade')->update($map);
   }
}
